<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Space>
          <a-button
            type="primary"
            :pre-icon="IconEnum.ADD"
            @click="handleAdd"
            v-auth="'system:role:edit'"
            >添加用户</a-button
          >
          <a-button
            :disabled="!selected"
            type="error"
            :pre-icon="IconEnum.CANCEL"
            @click="handleMultipleAuthCancel"
            v-auth="'system:role:edit'"
            >批量取消授权</a-button
          >
          <a-button type="warning" :ghost="true" :pre-icon="IconEnum.CLOSE" @click="handleClose"
            >关闭此页面</a-button
          >
        </Space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '取消授权',
                icon: IconEnum.CANCEL,
                type: 'primary',
                ghost: true,
                auth: 'system:role:edit',
                onClick: handleAuthCancel.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AssignRolesModal @register="registerModal" @reload="reloadWithCallback" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { roleAllocatedList, roleAuthCancel, roleAuthCancelAll } from '@/api/system/role';
  import { IconEnum } from '@/enums/appEnum';
  import { columns, formSchemas } from './assign.data';
  import { Space } from 'ant-design-vue';
  import { useRoute } from 'vue-router';
  import { ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import AssignRolesModal from './AssignRolesModal.vue';
  import { useTabs } from '@/hooks/web/useTabs';

  /** 拿到路由参数 */
  const roleId = ref<string>('0');
  const route = useRoute();
  roleId.value = route.params.roleId as string;

  const [registerTable, { reload, getSelectRowKeys, selected, reloadWithCallback }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    showIndexColumn: false,
    api: roleAllocatedList,
    rowKey: 'userId',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      baseColProps: {
        span: 6,
      },
    },
    // 额外的请求参数
    searchInfo: {
      roleId: roleId.value,
    },
    columns: columns,
    actionColumn: {
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });

  const [registerModal, { openModal }] = useModal();
  function handleAdd() {
    // 必须传参  否则无法触发回调
    openModal(true, {});
  }

  const { createConfirm } = useMessage();
  /**
   * 批量取消授权
   */
  function handleMultipleAuthCancel() {
    createConfirm({
      iconType: 'warning',
      title: '取消授权',
      content: '是否全部取消授权?',
      async onOk() {
        const userIds = getSelectRowKeys();
        await roleAuthCancelAll(roleId.value, userIds as number[]);
        await reload();
      },
    });
  }
  /**
   * 取消授权
   */
  function handleAuthCancel(record: Recordable) {
    createConfirm({
      iconType: 'warning',
      title: '取消授权',
      content: `是否取消授权用户[${record.userName} - ${record.nickName}]?`,
      async onOk() {
        await roleAuthCancel({ userId: record.userId, roleId: roleId.value });
        await reload();
      },
    });
  }

  const { closeCurrent } = useTabs();
  async function handleClose() {
    await closeCurrent();
  }
</script>

<style scoped></style>
